using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Browser;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Lib.Helper;
using MSIACCSL2.ViewModels;
using MSIACCSL2.Views.FormSearch;
using MSIACCSL2.Views.SalesOrder;
using MSIACCSL2.Views.Spk;
using MSIACCSL2.Views.SubGroup;
using WcfService1.DTO;

namespace MSIACCSL2.Views.Spk
{
    public partial class FrmAdd : Page
    {
        public SpkWrapViewModel SOW
        {
            get
            {
                return this.Resources["POW"] as SpkWrapViewModel;
            }
        }

        public SpkViewModel ViewModel
        {
            get
            {
                return this.DataContext as SpkViewModel;
            }
        }
    
        private SearchSubgroup sg;
        private SearchItem si;

        public FrmAdd()
        {
            InitializeComponent();
            System.Net.WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);

            this.Loaded += (x, y) =>
            {
                (this.Resources["POW"] as SpkWrapViewModel).SpkViewModel =
                     (SpkViewModel)this.DataContext;
            };

            EventAggregator.RegisterMessage("Refresh",(x)=>
                                                          {
                                                              this.DataContext = x;
                                                          });
   
           
          this.Loaded += (x, y) =>
                               {
                                   rbSubGroup.Focus();
                               };

            this.KeyDown += (x, y) =>
                                {
                                    if (y.Key == Key.T && Keyboard.Modifiers == ModifierKeys.Control)
                                        SOW.AddSpkDetail(null);
                                    else if (y.Key == Key.D && Keyboard.Modifiers == ModifierKeys.Control)
                                        SOW.DeleteSpkDetail(null);
                                    else if (y.Key == Key.S && Keyboard.Modifiers == ModifierKeys.Control)
                                        ViewModel.SaveSpk(ViewModel.Spk);
                                    else if (y.Key == Key.C && Keyboard.Modifiers == ModifierKeys.Control)
                                        ViewModel.Cancel(null);
                                };
            }

 
    

        private void textBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                SOW.GetInventory(((TextBox) sender).DataContext);
        }

        private void TextBox_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
        	(sender as TextBox).SelectAll();
        }


        //Lookup Subgroup
        private void KeyDo_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
        	if (e.Key == Key.Enter)
        	{
        	    sg = new SearchSubgroup();
                sg.Closed +=new EventHandler(sg_Closed);
                sg.txtSearch.GotFocus += (x, y) =>
                {
                    (x as TextBox).Text = (sender as TextBox).Text;
                    (x as TextBox).SelectAll();
                };
                sg.Show();
        	}

        }

        //Search Subgroup Finished
        private void sg_Closed(object sender, EventArgs e)
        {
            var Subgroup = (sender as SearchSubgroup).dg1.SelectedItem as SubGroupDTO;
            var spkDetail = GridSubgroup.SelectedItem as SpkDetailDTO;
            spkDetail.SubGroup = Subgroup;
            GridSubgroup.Focus();
        }

        //Lookup Item
        private void TxtName_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                ViewModel.GetItemByName(((TextBox)sender).DataContext);
                si = new SearchItem();
                si.Closed += new EventHandler(si_Closed);
                si.DataContext = ViewModel;
                si.Show();
            }
                
        }

        // Search Item Finished
        void si_Closed(object sender, EventArgs e)
        {
            GridItem.Focus();
        }
   
        //Lookup Inventory
        private void btnfind_Click(object sender, System.Windows.RoutedEventArgs e)
        {
           InventorySearch inventorySearch = new InventorySearch();
           inventorySearch.Pemanggil = typeof (SpkViewModel);
           inventorySearch.Closed += new EventHandler(inventorySearch_Closed);
           inventorySearch.Show();
        }


        //Search Inventory Finished
        void inventorySearch_Closed(object sender, EventArgs e)
        {

            var inv = (sender as InventorySearch).Inventory;
            if (inv == null) return;
            var SpkDetail = GridInventory.SelectedItem as SpkDetailDTO;
            SpkDetail.Inventory = inv;
            SpkDetail.Panjang = inv.Panjang;
            SpkDetail.Lebar = inv.Lebar;
            SpkDetail.Tinggi = inv.Tinggi;
            SOW.GetQtyUnit(SpkDetail);
            GridInventory.Focus();
        }

   

        private double GetVerticalOffset(FrameworkElement child, ScrollViewer scrollViewer)
        {
            GeneralTransform focusedVisualTransform = child.TransformToVisual(scrollViewer);
            Point topLeft = focusedVisualTransform.Transform(new Point(child.Margin.Left, child.Margin.Right));
            Rect rectangle = new Rect(topLeft, child.RenderSize);
            double newOffset = scrollViewer.HorizontalOffset + (rectangle.Right - scrollViewer.ViewportWidth);
            return newOffset < 0 ? 0 : newOffset; // no use returning negative offset 
        }

     

        private void KeyDo_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            (sender as TextBox).SelectAll();
        }

        private void TxtName_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            (sender as TextBox).SelectAll();
        }

        private void txtNoLog_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            (sender as TextBox).SelectAll();
        }

    }
}

